package com.aiyige.richtexteditor.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.aiyige.utils.ListUtil;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class RTEModel implements Parcelable {
    List<RTELocalMedia> rteLocalMediaList;
    boolean empty;
    String content;

    public RTEModel() {
    }

    public RTEModel(RTEModel other) {
        if (other != null) {
            this.rteLocalMediaList = ListUtil.isEmpty(other.rteLocalMediaList) ? new LinkedList<RTELocalMedia>() : new LinkedList<>(other.rteLocalMediaList);
            this.empty = other.empty;
            this.content = other.content;
        } else {
            this.rteLocalMediaList = new LinkedList<>();
            this.empty = true;
            this.content = "";
        }
    }

    private RTEModel(Builder builder) {
        setRteLocalMediaList(builder.rteLocalMediaList);
        setEmpty(builder.empty);
        setContent(builder.content);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public List<RTELocalMedia> getRteLocalMediaList() {
        return rteLocalMediaList;
    }

    public void setRteLocalMediaList(List<RTELocalMedia> rteLocalMediaList) {
        this.rteLocalMediaList = rteLocalMediaList;
    }

    public boolean isEmpty() {
        return empty;
    }

    public void setEmpty(boolean empty) {
        this.empty = empty;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public static final class Builder {
        private List<RTELocalMedia> rteLocalMediaList;
        private boolean empty;
        private String content;

        private Builder() {
            rteLocalMediaList = new LinkedList<>();
            empty = true;
            content = "";
        }

        public Builder rteLocalMediaList(List<RTELocalMedia> val) {
            rteLocalMediaList = val;
            return this;
        }

        public Builder empty(boolean val) {
            empty = val;
            return this;
        }

        public Builder content(String val) {
            content = val;
            return this;
        }

        public RTEModel build() {
            return new RTEModel(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(this.rteLocalMediaList);
        dest.writeByte(this.empty ? (byte) 1 : (byte) 0);
        dest.writeString(this.content);
    }

    protected RTEModel(Parcel in) {
        this.rteLocalMediaList = in.createTypedArrayList(RTELocalMedia.CREATOR);
        this.empty = in.readByte() != 0;
        this.content = in.readString();
    }


    public static final Creator<RTEModel> CREATOR = new Creator<RTEModel>() {
        @Override
        public RTEModel createFromParcel(Parcel source) {
            return new RTEModel(source);
        }

        @Override
        public RTEModel[] newArray(int size) {
            return new RTEModel[size];
        }
    };


    public void deleteRTELocalMediaByMediaId(String mediaId) {
        if (ListUtil.isEmpty(rteLocalMediaList)) return;
        Iterator<RTELocalMedia> iterator = rteLocalMediaList.iterator();
        while (iterator.hasNext()) {
            RTELocalMedia rteLocalMedia = iterator.next();
            if (rteLocalMedia.getMediaId().equals(mediaId)) {
                iterator.remove();
                return;
            }
        }
    }


}
